﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace XmlSockets
{
    internal struct ClientXmlSocketMessageBuffer
    {
        private XmlReader[] buffer_;
        private int count_;

        internal void InitBuffer(int size)
        {
            this.buffer_ = new XmlReader[size];
            count_ = 0;
            
        }



        internal void Append(XmlReader reader)
        {
            if (count_ >= this.buffer_.Length) ExtendBuffer();
            this.buffer_[count_] = reader;
            count_++;
        }

        internal XmlReader[] GetMessages()
        {
            if (count_ == 0) return null;
            XmlReader[] messages = new XmlReader[count_];
            Array.Copy(this.buffer_, 0, messages, 0, count_);
            Array.Clear(this.buffer_, 0, count_);
            count_ = 0;
            return messages;
        }

        private void ExtendBuffer()
        {
            XmlReader[] newBuffer = new XmlReader[this.buffer_.Length * 2 + 1];
            Array.Copy(this.buffer_, 0, newBuffer, 0, count_);
            this.buffer_ = newBuffer;
        }
    }
}
